package com.yb.xczx.cloud.media.service.impl;

import com.yb.core.enums.CommonAuditStatus;
import com.yb.core.enums.MediaFilesEnum;
import com.yb.core.util.AopUtil;
import com.yb.core.util.PathUtil;
import com.yb.core.exception.XczxBusinessException;
import com.yb.core.web.util.FileUtil;
import com.yb.xczx.cloud.media.entity.MediaFiles;
import com.yb.xczx.cloud.media.mapper.MediaFilesMapper;
import com.yb.xczx.cloud.media.model.dto.UploadMediaFilesDTO;
import com.yb.xczx.cloud.media.model.vo.UploadFileResultVo;
import com.yb.xczx.cloud.media.service.IMediaFilesService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yb.xczx.minio.upload.bean.MinioProperties;
import com.yb.xczx.upload.bean.UploadRequest;
import com.yb.xczx.upload.client.UploadClient;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.File;
import java.time.LocalDateTime;

/**
 * <p>
 * 媒资信息 服务实现类
 * </p>
 *
 * @author 姚兵
 * @since 2023-05-09
 */
@Service
@Slf4j
public class MediaFilesServiceImpl extends ServiceImpl<MediaFilesMapper, MediaFiles> implements IMediaFilesService {

    String FILENAME_PATTERN="chunks/%s/%s.%s";
    String URL_PATTERN="/%s/%s";
    @Autowired
    MinioProperties minioProperties;

    @Autowired
    UploadClient uploadClient;


    @Override
    public UploadFileResultVo upload(Long companyId, UploadMediaFilesDTO mediaFilesDTO) {

        log.info("companyId:{},mediaFilesDTO:{}",companyId,mediaFilesDTO);
        //获取数据
        String tempFilePath = mediaFilesDTO.getTempFilePath();
        Long fileSize = mediaFilesDTO.getFileSize();
        String remark = mediaFilesDTO.getRemark();
        String tags = mediaFilesDTO.getTags();
        String originFilename = mediaFilesDTO.getFilename();
        String fileMd5 = FileUtil.getMd5(new File(tempFilePath));
        String folder = PathUtil.getChunkFolder(fileMd5);
        String suffix = FileUtil.getSuffix(originFilename);
        String remoteFilename=String.format(FILENAME_PATTERN,folder,fileMd5,suffix);

        //上传远程

        UploadRequest uploadRequest = new UploadRequest()
                .setLocalFilepath(tempFilePath)
                .setContentType(FileUtil.getMimeType(originFilename))
                .setRemoteFilepath(remoteFilename)
                .setOriginFilename(originFilename)
                .setBucket(minioProperties.getFileBucket().getName());
        log.info("uploadRequest:{}",uploadRequest);
        if(!uploadClient.upload(uploadRequest).isSuccess()){
            log.error("upload failed");
            throw new XczxBusinessException("上传失败");
        }

        //保存数据库
        MediaFiles mediaFiles = new MediaFiles();
        mediaFiles.setCompanyId(companyId);
        mediaFiles.setFileSize(fileSize);
        mediaFiles.setFilename(originFilename);
        mediaFiles.setId(fileMd5);
        mediaFiles.setFileId(fileMd5);
        mediaFiles.setFilePath(remoteFilename);
        mediaFiles.setBucket(minioProperties.getFileBucket().getName());
        mediaFiles.setCreateDate(LocalDateTime.now());
        mediaFiles.setCreateDate(LocalDateTime.now());
        mediaFiles.setFileType(MediaFilesEnum.MediaType.IMAGE.getCode());
        mediaFiles.setStatus(MediaFilesEnum.Status.NORMAL.getCode());
        mediaFiles.setRemark(remark);
        mediaFiles.setTags(tags);
        mediaFiles.setAuditStatus(CommonAuditStatus.NOT_AUDIT.getCode());
        String url=String.format(URL_PATTERN,minioProperties.getFileBucket().getName(),remoteFilename);
        mediaFiles.setUrl(url);

        log.info("updateVo mediaFiles:{}",mediaFiles);

        IMediaFilesService currentProxy = AopUtil.getCurrentProxy();

        currentProxy.saveOrUpdate(mediaFiles);

        //封装返回结果
        UploadFileResultVo result = new UploadFileResultVo();
        BeanUtils.copyProperties(mediaFiles,result);
        return result;
    }
}
